package org.dingyom.basics.service.impl;

import org.dingyom.basics.service.IEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.UUID;
@Service
public class EmailServiceImpl implements IEmailService {

    @Autowired
    private JavaMailSender javaMailSender;
    /**
     * 发送邮件
     * @param shopId 店铺id
     * @param activeId 激活码
     * @param email 店铺管理员的邮箱
     */
    @Override
    public void send(Long shopId, String activeId, String email) {
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage, true, "utf-8");

            helper.setFrom("572282259@qq.com");//发件人邮箱
            helper.setSubject("店铺激活指南");
            String uid = UUID.randomUUID().toString();
            //http://img30.360buyimg.com/popWaterMark/jfs/t1/67988/7/14094/232759/5db64acfE6ab2b09e/38b5cb3dc38b4b1f.jpg"
            helper.setText("<h1>您的店铺已经通过了审核，请在30分钟内激活</h1>" +
                    "点击下方激活按钮，并输入激活码" +
                    "<div style='color: red'> "+activeId+"</div>" +
                    "<a href='http://localhost:8080/#/active/"+shopId+"'>激活</a>", true);
            //添加附件
//        helper.addAttachment("罗宾.jpg",new File("C:\\Users\\hm\\Desktop\\work\\aa.jpg"));
//        helper.addAttachment("压缩文件", new File("C:\\Users\\hm\\Desktop\\20191010\\2020-02-05-智能商贸-DAY4\\resources\\resources.zip"));
            //收件人
            helper.setTo(email);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void registerSuccess(String email, String content) {
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage, true, "utf-8");

            helper.setFrom("572282259@qq.com");//发件人邮箱
            helper.setSubject("用户注册成功");
            String uid = UUID.randomUUID().toString();
            //http://img30.360buyimg.com/popWaterMark/jfs/t1/67988/7/14094/232759/5db64acfE6ab2b09e/38b5cb3dc38b4b1f.jpg"
            helper.setText(content, true);
            //添加附件
//        helper.addAttachment("罗宾.jpg",new File("C:\\Users\\hm\\Desktop\\work\\aa.jpg"));
//        helper.addAttachment("压缩文件", new File("C:\\Users\\hm\\Desktop\\20191010\\2020-02-05-智能商贸-DAY4\\resources\\resources.zip"));
            //收件人
            helper.setTo(email);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        javaMailSender.send(mimeMessage);
    }
}
